﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace Hack.Numbers
{
    public class RandomNumberGenerator
    {
        //Returns a single random integer between two numbers, both inclusive        
        public static int GetRandomInt(int min, int max)
        {
            return GetRandomInts(min, max, 1)[0];
        }

        //Returns an array of random integers between two numbers, both inclusive        
        public static int[] GetRandomInts(int min, int max, int trials)
        {
            //Build the url string to www.random.org
            string url = "http://www.random.org/integers/?num=" + Convert.ToString(trials);

            url += "&min=" + Convert.ToString(min);
            url += "&max=" + Convert.ToString(max);
            url += "&col=1&base=10&format=html&rnd=new";

            string data = DownloadData(url);

            if (data != string.Empty)
            {
                //Parse the data
                string startMarker = "<pre class=" + '"' + "data" + '"' + ">"; //<pre class="data">
                int j = data.IndexOf(startMarker);
                if (j != -1)
                {
                    int k = data.IndexOf("</pre>", j);
                    if (k != -1)
                    {
                        string intString = data.Substring(j + startMarker.Length, k - j - startMarker.Length);
                        intString = intString.Trim();

                        //Read each line
                        List<int> integers = new List<int>();
                        StringReader readLines = new StringReader(intString);

                        while (readLines.Peek() != -1)
                        {
                            integers.Add(int.Parse(readLines.ReadLine()));
                        }

                        return integers.ToArray();
                    }
                }
            }

            return new int[] { -1 };
        }

        public static bool HasQuota()
        {
            string url = "http://www.random.org/quota/?format=plain";

            int ret = Convert.ToInt32(DownloadData(url));

            return (ret > 0);
        }

        //Connects to URL to download data
        private static string DownloadData(string url)
        {
            try
            {
                //Get a data stream from the url
                WebRequest req = WebRequest.Create(url);
                WebResponse response = req.GetResponse();
                Stream stream = response.GetResponseStream();

                //Download in chuncks
                byte[] buffer = new byte[1024];

                //Get Total Size
                int dataLength = (int)response.ContentLength;

                //Download to memory
                //Note: adjust the streams here to download directly to the hard drive
                MemoryStream memStream = new MemoryStream();
                while (true)
                {
                    //Try to read the data
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);

                    if (bytesRead == 0)
                        break;
                    else
                        memStream.Write(buffer, 0, bytesRead);
                }

                //Convert the downloaded stream to a byte array
                string downloadedData = System.Text.ASCIIEncoding.ASCII.GetString(memStream.ToArray());

                //Clean up
                stream.Close();
                memStream.Close();

                return downloadedData;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

    }

}
